package com.twu.biblioteca;

import java.util.Scanner;

public class ReserveBookAction implements Action {
    private Library library;

    public ReserveBookAction(Library library) {
        this.library = library;
    }

    public String execute() {
        String result;
        System.out.println("\nPlease input the id of the book you want to reserve:");

        int bookId = getInputBookId();
        Book book = findBookById(bookId);
        if (book != null && book.isAvailable()) {
            book.setAvailable(false);
            result = "Thank You! Enjoy the book.";
        } else {
            result = "Sorry we don't have that book yet.";
        }
        return result;
    }

    public int getInputBookId() {
        int bookId = 0;
        Scanner scanner = new Scanner(System.in);
        if (scanner.hasNextInt()) {
            bookId = scanner.nextInt();
        }
        return bookId;
    }

    public Book findBookById(int bookId) {
        for (Book book : library.getBooks()) {
            if (book.getBookId() == bookId) {
                return book;
            }
        }
        return null;
    }
}
